package com.dt.starter;

import com.bstek.uflo.console.UfloServlet;
import com.bstek.uflo.console.handler.impl.designer.DesignerServletHandler;
import com.bstek.uflo.service.ProcessService;
import com.dt.module.flow.service.impl.SysUfloProcessService;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTransactionManager;

import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class UfloConfig {


    @Bean
    public ServletRegistrationBean<Servlet> ureportServlet(){

        ServletRegistrationBean bean=new ServletRegistrationBean(new UfloServlet(), "/uflo/*");
        return bean;
    }


    @Bean("localSessionFactoryBean")
    public LocalSessionFactoryBean localSessionFactoryBean(DataSource dataSource) throws
            PropertyVetoException, IOException {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setPackagesToScan("com.bstek.uflo.model*");
        Properties prop = new Properties();
        prop.put("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
        prop.put("hibernate.show_sql",true);
        prop.put("hibernate.format_sql",true);
        prop.put("hibernate.hbm2ddl.auto","none");
        prop.put("hibernate.cache_use_query.cache",false);
        prop.put("hibernate.jdbc.batch_size",100);
//        prop.put("hibernate.current_session_context_class","thread");
        sessionFactoryBean.setHibernateProperties(prop);
        return sessionFactoryBean;
    }


    @Bean("ufloTransactionManager")
    public HibernateTransactionManager ufloTransactionManager(SessionFactory sessionFactory){
        HibernateTransactionManager hi = new HibernateTransactionManager();
        hi.setSessionFactory(sessionFactory);
        return hi;
    }



}
